<?php

namespace app\daren\model\daren;

use app\user\model\User;
use sheep\model\Common;
use think\model\concern\SoftDelete;

class Daren extends Common
{
    use SoftDelete;

    protected $name = 'daren';

    public function getDarenTypeAttr($value)
    {
        $status = [
            'group' => '团购',
            'scm' => '供应链',
        ];
        return ['text' => $status[$value], 'value' => $value];
    }

    public function getPlatformAttr($value)
    {
        $platform = [
            'douying' => '抖音',
            'kuaishou' => '快手',
        ];
        return ['text' => $platform[$value], 'value' => $value];
    }

    public function getSexAttr($value)
    {
        $sex = [
            'boy' => '男',
            'girl' => '女',
            'other' => '其他',
        ];
        return ['text' => $sex[$value], 'value' => $value];
    }

    public function getAuditStatusAttr($value)
    {
        $status = [
            'pending' => '待审核',
            'pass' => '已通过',
            'back' => '已拒绝',
        ];
        return ['text' => $status[$value], 'value' => $value];
    }

    /**
     * 达人审核列表
     * @param $param
     * @return \think\Paginator
     * @throws \think\db\exception\DbException
     */
    public function getList($param = [])
    {
        $model = $this;
        $where = [];
        if (!empty($param['audit_status'])) {
            $where[] = ['audit_status', '=', $param['audit_status']];
        }

        if (!empty($param['daren_id'])) {
            $where[] = ['id', '=', $param['daren_id']];
        }
        // 账号
        if (!empty($param['platform_account'])) {
            $where[] = ['platform_account', '=', $param['platform_account']];
        }
        // 联系电话
        if (!empty($param['phone_number'])) {
            $where[] = ['phone_number', 'like', '%' . $param['phone_number'] . '%'];
        }
        // 接单范围
        if (!empty($param['order_range'])) {
            $where[] = ['order_range', 'like', '%' . $param['order_range'] . '%'];
        }
        // 平台
        if (!empty($param['platform'])) {
            $where[] = ['platform', '=', $param['platform']];
        }

        // 创建时间
        if (!empty($param['start_time'])) {
            $where[] = ['create_time', '>', strtotime($param['start_time'])];
        }
        if (!empty($param['end_time'])) {
            $where[] = ['create_time', '<=', strtotime($param['end_time'])];
        }

        if (!empty($param['commerce_level_1']) && !empty($param['commerce_level_2'])) {
            $where[] = ['commerce_level', 'between', [$param['commerce_level_1'], $param['commerce_level_2']]];
        }

        return $model->with('user')->order('id desc')->where($where)->paginate($param);
    }

    /**
     * 详情
     * @param $param
     * @return \think\Paginator
     * @throws \think\db\exception\DbException
     */
    public function detail($id)
    {
        return $this->with('user')->where('id', $id)->find();
    }

    /**
     * 状态修改
     * @param $data
     * @return Daren|\think\Model
     */
    public function dataStatusEdit($data)
    {
        $darenModel = self::find($data['id']);
        if (!$darenModel) {
            return error('数据不存在');
        }
        $darenModel->audit_status = $data['audit_status'];
        $darenModel->remark = $data['remark'];
        $darenModel->pass_time = time();
        $darenModel->save();
        return $darenModel;
    }


    // 获取器：将 pass_time 转换为日期格式
    public function getPassTimeAttr($value)
    {
        return $value ? date('Y-m-d H:i:s', $value) : null;
    }


    public function user()
    {
        return $this->hasOne(User::class, 'id', 'user_id');
    }



}